<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Layout\Content;
use App\Admin\Utils\Config;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ConfigController extends Controller
{
    /**
     * @var Config
     */
    private $config;

    /**
     * @return Config
     */
    private function config()
    {
        // 如果把config放到构造函数里,那时候File组件获取不到csrf_token
        return $this->config ?: $this->config = app('AdminConfig');
    }

    public function index()
    {
        return \Admin::content(function (Content $content) {
            $content->header('系统设置');
            $content->description('');

            $content->body($this->config()->renderForm());
        });
    }

    public function store(Request $request)
    {
        $success = $this->config()->store($request);

        if ($success) {
            flash_msg('更新成功');

            return response()->json([
                'status' => true,
                'action' => route('admin.configs.index'),
            ]);
        }
    }

    public function upload()
    {
        return $this->config()->upload();
    }
}
